package com.itheima.ydd.controller;

import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.SysUserInfo;
import com.itheima.ydd.entity.User;
import com.itheima.ydd.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;


/**
 * 管理员管理
 */
@RestController
@Slf4j
@RequestMapping("sys_user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 分页查询
     *
     * @return
     */
    @GetMapping("list")
    public ResultData list(String login_name, String phone, String status, Integer page, Integer limit) {
        log.info("分页查询login_name:{},phone:{},status:{}", login_name, phone, status);
        HashMap<String, Object> map = userService.getPageHashMap(login_name, phone, status, page, limit);
        return ResultData.ok("", map);
    }


    /**
     * 用户删除
     *
     * @param id
     * @return
     */
    @GetMapping("del")
    public ResultData delete(String id) {
        log.info("用户删除id:{}", id);
        userService.removeById(id);
        return ResultData.ok("", "用户删除成功！");
    }

    /**
     * 用户添加
     *
     * @param sysUserInfo
     * @return
     */
    @PostMapping("add")
    public ResultData add(@RequestBody SysUserInfo sysUserInfo) {
        log.info("用户添加SysUserInfo:{}", sysUserInfo);
        userService.add(sysUserInfo);
        return ResultData.ok("", "添加成功");
    }


    /**
     * 返回管理员详情
     *
     * @param id
     * @return
     */
    @GetMapping("rowInfo")
    public ResultData rowInfo(String id) {
        log.info("返回管理员详情id:{}", id);
        User byId = userService.getById(id);
        return ResultData.ok("", byId);
    }

    /**
     * 管理员修改
     *
     * @param sysUserInfo
     * @return
     */
    @PostMapping("update")
    public ResultData update(@RequestBody SysUserInfo sysUserInfo) {
        log.info("管理员修改SysUserInfo:{}", sysUserInfo);
        userService.updateUser(sysUserInfo);
        return ResultData.ok("", "修改成功");
    }


}
